<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="tree-card">
        <!-- 用了一个行列布局 -->
        <TreeTools :node-data="{name:'江苏传智播客教育科技股份有限公司',manager:'负责人',id:''}" :is-root="true" @add="addDepartment" />
        <hr>

        <el-tree
          :data="depts"
          :expand-on-click-node="false"
          :props="{
            label:'name'
          }"
        >
          <template #default="obj">
            <TreeTools :node-data="obj.data" @del="getDepartments" @add="addDepartment" @edit="getDepartmentDatail" />
          </template>
        </el-tree>
      </el-card>
      <addDept ref="edit" :is-show-dialog.sync="isShowDialog" :node-data="nodeData" @addDepartments="addDepartments" />
    </div>
  </div>
</template>

<script>
import addDept from './components/add-dept.vue'
import TreeTools from './components/tree-tools.vue'
import { getDepartments } from '@/api/department'
import { listToTreeData } from '@/utils/index'
export default {
  components: { TreeTools, addDept },
  data() {
    return {
      depts: [],
      isShowDialog: false,
      nodeData: {}
    }
  },
  async created() {
    this.getDepartments()
  },
  methods: {
    async getDepartments() {
      const { depts } = await getDepartments()
      this.depts = listToTreeData(depts, '')
    },
    addDepartment(nodeData) {
      this.isShowDialog = true
      this.nodeData = nodeData
    },
    addDepartments() {
      this.getDepartments()
      this.$message.success('添加成功')
    },
    getDepartmentDatail(nodeData) {
      this.nodeData = nodeData
      this.isShowDialog = true
      console.log(223)
      this.$refs.edit.getDepartmentDatail(this.nodeData.id)
    }
  }
}
</script>

<style scoped>
.tree-card {
  padding: 30px  140px;
  font-size:14px;

}
</style>

